﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVatTu.Class;

namespace QLVatTu.HeThong
{
    public partial class frmLichSuHeThong : Form
    {
        KetNoiDB cn = new KetNoiDB();

        public frmLichSuHeThong()
        {
            InitializeComponent();
        }

        private void loadData()
        {
            cbbQuyenHan.DataSource = cn.getTable("select * from tbl_QuyenHan");
            cbbQuyenHan.DisplayMember = "TenQuyenHan";
            cbbQuyenHan.ValueMember = "MaQuyenHan";

            cbbTenNguoiDung.DataSource = cn.getTable("select *from tbl_NhanVien");
            cbbTenNguoiDung.DisplayMember = "TenNhanVien";
            cbbTenNguoiDung.ValueMember = "MaNhanVien";

            load_dgvLichSu();
        }

        private void load_dgvLichSu(string dieuKien=null)
        {
            string sql="";
            if (dieuKien != null)
            {
                sql = @"select * from tbl_LichSuHeThong
                        "
                        + dieuKien;
            }
            else
            {
                sql = "select * from tbl_LichSuHeThong";
            }
            DataTable tbl = cn.getTable(sql);
            dgvLichSu.DataSource = tbl;
        }

        private void frmLichSuHeThong_Load(object sender, EventArgs e)
        {
            if (cn.KetNoi())
            {
                loadData();
            }
        }

        private void ckbThoiGian_CheckedChanged(object sender, EventArgs e)
        {
            dtpTuNgay.Enabled = ckbThoiGian.Checked;
            dtpDenNgay.Enabled = ckbThoiGian.Checked;
        }

        private void ckbTenDangNhap_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbTenDangNhap.Checked == true)
            {
                txtTenDangNhap.Text = "";
                txtTenDangNhap.Enabled = true;
            }
            else
            {
                txtTenDangNhap.Text = "Tìm theo tên đăng nhập";
                txtTenDangNhap.Enabled = false;
            }
        }

        private void ckbQuyenHan_CheckedChanged(object sender, EventArgs e)
        {
            cbbQuyenHan.Enabled = ckbQuyenHan.Checked;
        }

        private void ckbTenNguoiDung_CheckedChanged(object sender, EventArgs e)
        {
            cbbTenNguoiDung.Enabled = ckbTenNguoiDung.Checked;
        }

        private void ckbHanhDong_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbHanhDong.Checked == true)
            {
                txtHanhDong.Text = "";
                txtHanhDong.Enabled = true;
            }
            else
            {
                txtHanhDong.Text = "Tìm theo hành động";
                txtHanhDong.Enabled = false;
            }
        }

        private string XacDinhDieuKienLoc()
        {
            string dieuKienLoc = "where 1=1";

            if (ckbThoiGian.Checked == true)
            {
                dieuKienLoc += "and ThoiGian between '"+dtpTuNgay.Value.ToString()+"' and '"+dtpDenNgay.Value.ToString()+"'";
            }

            if (ckbTenDangNhap.Checked == true && txtTenDangNhap.Text != "")
            {
                dieuKienLoc += "and TenDangNhap like '%"+txtTenDangNhap.Text+"%'";
            }

            if (ckbQuyenHan.Checked == true && cbbQuyenHan.Items.Count>0)
            {
                dieuKienLoc += "and TenQuyenHan=N'"+cbbQuyenHan.Text+"'";
            }

            if (ckbTenNguoiDung.Checked == true && cbbTenNguoiDung.Items.Count > 0)
            {
                dieuKienLoc += "and TenNguoiDung=N'"+cbbTenNguoiDung.Text+"'";
            }

            if (ckbHanhDong.Checked == true && txtHanhDong.Text != "")
            {
                dieuKienLoc += "and HanhDong like '%"+txtHanhDong.Text+"%'";
            }

            return dieuKienLoc;
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            load_dgvLichSu(XacDinhDieuKienLoc());
        }

        private void radLoad_CheckedChanged(object sender, EventArgs e)
        {
            radLoad.Checked = false;
            load_dgvLichSu();
            ckbHanhDong.Checked = false;
            ckbQuyenHan.Checked = false;
            ckbTenDangNhap.Checked = false;
            ckbTenNguoiDung.Checked = false;
            ckbThoiGian.Checked = false;
        }

    }
}
